In this video we are going to create the equal weighted portfolio consisting of our six portfolio stocks
and equal weighted means that at each timestamp the weight of each single stock in the portfolio is
one divided by six.
So let's first of all import pen us and we also import an umpire and met plot lip and also seaborne
and we define that float object.
So to have uh two decimals.
So let's do this and then we import our portfolio stocks from the portfolio of stocks.
Yes we file and we create a day time index and we save uh the data frame and the value of stocks.
So let's have a look.
So these are the adjusted to close prices over time and actually calculating the return of an equal
weighted portfolio is pretty simple.
So we simply have to calculate the mean return of our six stocks on each timestamp and therefore first
of all we create the returns data frame with the percentage change method and we save the returns data
frame and the variable red so this is the returns data frame as the daily returns.
And as I said to get the daily returns for our equal weighted portfolio we simply have to calculate
here the average are the mean return of the six stocks on each timestamp so to say we have to calculate
the row vice mean and we can do this with the mean method and pass on to the access parameter to get
the mean values per Roe so these are the mean values are the mean returns pair Roe up her timestamp
and with this we are finished here.
That's uh that daily returns of the equal weighted portfolio but as the next step we also want to create
portfolios that are not equal weighted and therefore we have to derive a more general way to get to
the weighted average return on each timestamp.
And uh let's have a look here.
So first of all we need to define the amount of assets or the amount of stocks that should be in our
portfolio.
So in this case we are working with six stocks and we actually can determine the length of our columns
index.
So no surprise we have six and the next step we create a list with the weights of our 6 stocks.
So with the equal weighted portfolio we have six times the weight of fund divided by six.
So we are creating the list weights.
And this is a list comprehension.
So for you and range six or six times we create 1 divided by 6.
So let's have a look.
So you have six times the rate one divided by six.
And now we want to calculate the evaded average return for all time stems and therefore on the first
step we have to multiply the return of each stock.
So here these are the daily returns and we have to multiply them by the respective weights here and
uh we have to do this for each timestamp.
And finally we have to sum up then the weighted daily returns to get uh the weighted average return
for the six stocks.
And uh we can do this year and two steps are first of all we have our returns data frame and we multiply
the returns data frame with the weights list and we pass columns to the access parameter because we
want to apply here the respective weights on our columns.
So here the first weight should always be applied on the Amazon stock for example.
So let's do this here.
So these are the evaded daily returns and to get the weighted average.
We have to take the sum so we use the same method and pass one to the access parameter to uh some of
the data frame upper row wise or per row.
And again here we have for the daily returns for our equal weighted portfolio.
And as you can see here we change here two methods.
So the model method and the sum method and we can actually combine these two steps into one step by
using the dot product on a matrix multiplication and we can do this with the top method.
So we take our returns data frame and we apply a matrix multiplication with the top method and we pass
our weights the list.
So this gives us actually the same result.
So here we have the daily returns of the equal weighted portfolio.
And finally we create a new column equal weighted portfolio for our returns data frame by using exactly
the code so let's do this and let's have a look.
So here we have the six constituents of our portfolio and the equal weighted portfolio and for all of
them we feel that daily returns on each timestamp.
And now as a next step we want to aggregate here the daily returns.
And we want to calculate the annualized mean return and the annualized standard deviation of returns.
And therefore we create here the summary data frame.
So first of all we calculate the mean return of our daily returns and the standard deviation of daily
returns.
And we use here the ACT method.
So let's do this and let's have a look.
So we actually transposed the data frame in order to FESA the index our stocks and our portfolio and
as columns.
The mean return and the standard deviation of returns.
And then it's definitely a good idea to rename the column labels to return and risk and still we have
yeah the mean and the standard deviation of daily returns.
And uh it's more intuitive to analyze them so we multiply the mean return with 252.
So that's the average amount of trading days.
And we multiply the risk by the square root of 252.
So this is nothing new.
And finally we feel our summary data frame with the annualized return and the annualized risk.
And finally we can also create a scatter plot.
So we use the plot method on our summary data frame and on the x axis we have the risk column and on
the y axis the return column.
So we actually should get the seven dots seven points here and our scatter plot.
And we also annotate the label for each point for each dot here.
So that's the take us and the equal weighted portfolio.
And let's have a look here.
So here we have the total risk return profile for our six stocks and for our equal weighted portfolio.
So this is also nothing new here.
So here we have our equal weighted portfolio and these are our six constituents.
And finally to save some time and some code in our next videos we define a function annualized the risk
and return where we pass a return data frame.
So for example here this is our returns data frame and we pass the return data frame to the function
and actually the function calculates the mean and the standard deviation and it renames the columns
and annualize this risk and return.
And then finally returns the final summary data frame.
So we are defining the function here for the next videos to save some code and some time.
And uh with uh this we are finishing with this video and I hope to see you also in the next one by.